package net.ibizsys.eclipse.studio.vc;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.ibizsys.eclipse.studio.Activator;
import net.ibizsys.eclipse.studio.console.Console;
import net.ibizsys.eclipse.studio.data.Project;
import net.ibizsys.eclipse.studio.data.IBizSystem;
import net.ibizsys.eclipse.studio.data.IBizUser;
import net.ibizsys.eclipse.studio.vc.git.GitManager;
import net.ibizsys.eclipse.studio.vc.svn.SVNManager;

/**
 * [系统]版本控制管理对象
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class SystemVCManager
{
	public static final String CommitInfo = "";

	private static Map<String, IVCManager> map = new HashMap<String, IVCManager>();

	public static void build()
	{
		map.put(VCType.SVN.value(), new SVNManager());
		map.put(VCType.GIT.value(), new GitManager());
	}

	/**
	 * 获取版本控制对象
	 * 
	 * @param type 版本控制类型
	 * @return
	 */
	public static IVCManager getVCManager(VCType type)
	{
		if (map.containsKey(type.value()))
		{
			return map.get(type.value());
		}
		return null;
	}

	/**
	 * 获取版本控制对象
	 * 
	 * @param repository 仓库地址
	 * @return
	 */
	public static IVCManager getVCManager(String repository)
	{
		if (repository.endsWith(".git"))
		{
			return getVCManager(VCType.GIT);
		} else
		{
			return getVCManager(VCType.SVN);
		}
	}

	/**
	 * 忽略文件\文件夹
	 * 
	 * @param system
	 * @param projects
	 * @return
	 */
	public static void ignore(String svnUrl, String path)
	{
		IBizUser user = Activator.getDefault().curUser();

		VCRepository repository = new VCRepository(svnUrl, user.getSvnAccount(), user.getSvnPassword());
		try
		{
			getVCManager(svnUrl).ignore(repository, path, new IVCActionListener()
			{
				@Override
				public void onNotify(String info)
				{
					Console.println(info + "");
				}
			});
		} catch (Exception e)
		{
			Console.println("SVN忽略文件异常[" + path + "]异常.");
			Console.println(e.getMessage());
		}
	}

	/**
	 * 检出系统
	 * 
	 * @param system
	 * @param projects
	 * @return
	 */
	public static List<Project> checkout(IBizSystem system, List<Project> projects)
	{
		List<Project> checkoutList = new ArrayList<Project>();

		if (projects == null)
		{
			return checkoutList;
		}

		IBizUser user = Activator.getDefault().curUser();

		for (Project project : projects)
		{
			VCRepository repository = new VCRepository(project.getSVNUrl(), user.getSvnAccount(), user.getSvnPassword());

			File destPath = new File(Activator.getDefault().getRootPath() + File.separator + project.getName());

			if (destPath.exists())
			{
				Console.println("[检出]由于已经存在忽略项目[" + project.getName() + "],如需检出请先删除再执行[检出]命令.");
				continue;
			}

			try
			{
				Console.println("[检出]正在检出项目[" + project.getName() + "].");
				getVCManager(system.getSvnroot()).checkout(repository, null, destPath.getAbsolutePath(), new IVCActionListener()
				{
					@Override
					public void onNotify(final String info)
					{
						Console.println(info + "");
					}
				});
				checkoutList.add(project);
			} catch (Exception e)
			{
				Console.println("检出项目[" + project.getName() + "]异常.");
				Console.println(e.getMessage());
			}
		}
		Console.println("检出系统完成.");

		return checkoutList;
	}

	/**
	 * 更新[系统]的项目代码
	 * 
	 * @param system
	 * @param projects
	 */
	public static void update(IBizSystem system, List<Project> projects)
	{
		IBizUser user = Activator.getDefault().curUser();

		for (Project project : projects)
		{
			VCRepository repository = new VCRepository(project.getSVNUrl(), user.getSvnAccount(), user.getSvnPassword());

			File destPath = new File(Activator.getDefault().getRootPath() + File.separator + project.getName());

			if (!destPath.exists())
			{
				continue;
			}

			try
			{
				Console.println("[更新]正在更新项目[" + project.getName() + "].");

				getVCManager(system.getSvnroot()).update(repository, destPath.getAbsolutePath(), new IVCActionListener()
				{
					@Override
					public void onNotify(final String info)
					{
						Console.println(info);
					}
				});
				Console.println("更新项目[" + project.getName() + "]结束.");
			} catch (Exception e)
			{
				Console.println("更新项目[" + project.getName() + "]异常.");
				Console.println(e.getMessage());
			}
		}

		Console.println("[更新]命令执行完毕.");
	}

	/**
	 * 提交[系统]的项目代码
	 * 
	 * @param system
	 * @param projects
	 */
	public static void commit(IBizSystem system, List<Project> projects)
	{
		if (projects == null || projects.size() == 0)
		{
			Console.println("[提交]当前系统未检出项目在本地,无法提交.");
			return;
		}

		IBizUser user = Activator.getDefault().curUser();

		for (Project project : projects)
		{
			VCRepository repository = new VCRepository(project.getSVNUrl(), user.getSvnAccount(), user.getSvnPassword());

			File destPath = new File(Activator.getDefault().getRootPath() + File.separator + project.getName());

			if (!destPath.exists())
			{
				continue;
			}

			try
			{
				Console.println("[提交]正在提交项目[" + project.getName() + "].");

				getVCManager(system.getSvnroot()).commit(repository, destPath.getAbsolutePath(), CommitInfo, new IVCActionListener()
				{
					@Override
					public void onNotify(final String info)
					{
						Console.println(info + "");
					}
				});
				Console.println("提交项目[" + project.getName() + "]结束.");
			} catch (Exception e)
			{
				Console.println("提交项目[" + project.getName() + "]异常.");
				Console.println(e.getMessage());
			}
		}
		Console.println("[提交]命令执行完毕.");
	}
}
